/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.BorrowerDAO;
import com.cashmate.hibernate.pojo.Borrowers;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */

@Action(value = "/get-bcode", results = {
    @Result(name = "success", type = "stream", params = {"inputName", "inputStream", "contentType", "text/html"}),
    @Result(name = "error", location = "WEB-INF/error/indexError.html")
})
public class GetBorrowerCode extends ActionSupport {

    private static Logger logger = Logger.getLogger(GetBorrowerCode.class);
    private BorrowerDAO bo = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private InputStream inputStream;
    private Borrowers bor;
    private String bname;
    private String borrowername;

    public void setBorrowername(String borrowername) {
        this.borrowername = borrowername;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }
  

    public InputStream getInputStream() {
        return inputStream;
    }

    @Override
    public String execute() {

        try {
            String[] splitname = bname.split(",");
            String lname = splitname[0].trim();
            String fname = splitname[1].trim();
            bor = bo.getBorrowerByName(lname, fname);

            //String name = String.valueOf(bor.getBorrowerid());
            //inputStream = new StringBufferInputStream(name);
             inputStream = new StringBufferInputStream(bor.getBorrowerid().toString());

        } catch (Exception e) {
//            System.out.println("error sa getborrowercode:");
            e.printStackTrace();
        }
        return SUCCESS;
    }
}
